import { _intro, _check } from '../../common/api/test'
import { formatDate } from '../../utils/util'
const app = getApp()
Page({
  data: {
    id: null,
    info: null,
    already: false
  },
  totalQuery () {
    let memberid = wx.getStorageSync('memberid')
    app.loading('加载中')
    Promise.all([
      _intro(this.data.id),
      _check(memberid, this.data.id)
    ]).then(res => {
      wx.hideLoading()
      let info = res[0].data.Data
      info.EndTime = formatDate(new Date(info.EndTime), 'yyyy-MM-dd')
      info.Statime = formatDate(new Date(info.Statime), 'yyyy-MM-dd')
      // 是否已经参与
      let already = res[1].data.IsSuccess
      this.setData({
        info,
        already
      })
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求错误，请稍后再试',
        showCancel: false
      })
    })
  },
  goTest () {
    if (this.data.already==false) {
      wx.showModal({
        title: '温馨提示',
        content: '您已参与过本项考试，\n请点击【查看成绩】',
        showCancel: false
      })
    } else {
      wx.navigateTo({
        url: `./test?id=${this.data.id}`
      })
    }
  },
  goResult () {
    if (this.data.already==false || this.data.type === '模拟考试') {
      wx.navigateTo({
        url: `./result?id=${this.data.id}`
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '您尚未参与过本项考试，\n请点击【开始考试】',
        showCancel: false
      })
    }
  },
  onLoad (options) {
    this.data.id = options.id
    this.data.type = options.type
  },
  onReady () {},
  onShow () {
    if (app.store.userinfo) {
      this.data.userinfo = app.store.userinfo
      this.totalQuery()
    } else {
      app.loading('加载中')
      app.launch().then(res => {
        wx.hideLoading()
        console.log(res)
        this.data.userinfo = res
        this.totalQuery()
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
      })
    }
  },
  onHide () {},
  onUnload () {},
  onPullDownRefresh () {},
  onReachBottom () {},
  onShareAppMessage () {}
})